//
//  DatePickView.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/20.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "DatePickView.h"

@interface DatePickView()

@property(nonatomic,strong)NSDate *SelectDate;

@property(nonatomic,copy)DatePickBlock Pblock;

@end

@implementation DatePickView

-(instancetype)initWithFrame:(CGRect)frame andDate:(NSDate*)OrginDate andblock:(DatePickBlock)block
{
    if (self=[super initWithFrame:frame]) {
        
        self=[[[NSBundle mainBundle]loadNibNamed:@"DatePickView" owner:self options:nil]lastObject];
        
        self.frame=frame;
        
        self.SelectDate=OrginDate;

        self.Pblock =block;
        
        [self.DatePickView setDate:self.SelectDate animated:YES];
        
        [self.DatePickView addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        
    }
    
    return self;
}

-(void)dateChanged:(UIDatePicker*)sender{
    
    NSDate *date=sender.date;
    
    self.SelectDate=date;
    
    
}



-(void)PopWithView:(UIView*)SupView
{
    
    [SupView addSubview:self];
    
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.0);
    
    BaseA.toValue=@(0.3);
    
    BaseA.duration=0.3;
    
    [self.BlackBgView pop_addAnimation:BaseA forKey:@"fade"];
    
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentBgView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentBgView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentBgView pop_addAnimation:BaseB forKey:@"dy"];
    
}

-(void)DissmissSupview
{
    POPBasicAnimation *BaseA=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    
    BaseA.fromValue=@(0.3);
    
    BaseA.toValue=@(0.0);
    
    BaseA.duration=0.3;
    
    [self.BlackBgView pop_addAnimation:BaseA forKey:@"fade"];
    
    POPBasicAnimation *BaseB=[POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
    
    BaseB.fromValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height-self.ContentBgView.height/2.0)];
    
    BaseB.toValue=[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0, self.height+self.ContentBgView.height/2.0)];
    
    BaseB.duration=0.3;
    
    [self.ContentBgView.layer pop_addAnimation:BaseB forKey:@"dy"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self removeFromSuperview];
        
    });
    
    
}

- (IBAction)SureAction:(UIButton *)sender {
    
    self.Pblock(self.SelectDate);
    
    [self DissmissSupview];
    
}
- (IBAction)CleanAction:(UIButton *)sender {
    
    
    self.Pblock(nil);
    
     [self DissmissSupview];
    
}
- (IBAction)CancleAction:(UIButton *)sender {
    
    
     [self DissmissSupview];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    
    if (![touch.view isEqual:self.ContentBgView]) {
        
        [self DissmissSupview];
    }
    
}

@end
